package com.woan.api.services;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.woan.api.idao.SysAdmininfoIDAO;
import com.woan.api.idao.SysAdmininfoMobileIDAO;
import com.woan.api.idao.SysRoleIDAO;
import com.woan.api.model.SysAdmininfo;
import com.woan.api.model.SysAdmininfoMobile;
import com.woan.api.model.SysMessage;
import com.woan.api.model.SysRole;
import com.woan.api.util.CommonUtil;
import com.woan.api.util.FuncDate;
import com.woan.api.vo.Json;
import com.woan.api.vo.VadmininfoSimpleList;
import com.woan.api.vo.VloginList;
import com.woan.api.vo.VvideoList;

import net.sf.json.JSONObject;
public class UserClientServices {
	private SysAdmininfoIDAO sysadmininfoiDAO;
	private SysAdmininfoMobileIDAO sysadmininfomobileiDAO;
	private SysRoleIDAO sysroleiDAO;
	public SysAdmininfoIDAO getSysadmininfoiDAO() {
		return sysadmininfoiDAO;
	}
	public void setSysadmininfoiDAO(SysAdmininfoIDAO sysadmininfoiDAO) {
		this.sysadmininfoiDAO = sysadmininfoiDAO;
	}
	public SysAdmininfoMobileIDAO getSysadmininfomobileiDAO() {
		return sysadmininfomobileiDAO;
	}
	public void setSysadmininfomobileiDAO(
			SysAdmininfoMobileIDAO sysadmininfomobileiDAO) {
		this.sysadmininfomobileiDAO = sysadmininfomobileiDAO;
	}
	public SysRoleIDAO getSysroleiDAO() {
		return sysroleiDAO;
	}
	public void setSysroleiDAO(SysRoleIDAO sysroleiDAO) {
		this.sysroleiDAO = sysroleiDAO;
	}
	//组注册
	public JSONObject groupRegist(String groupname,String groupdesc,String grouppwd,String usercount)
	{
		JSONObject json=new JSONObject();
		boolean flag = false;
		if(usercount==null || usercount.equals(""))
		{
			usercount="10";
		}
		if(groupname.equals(""))
		{
			json.accumulate("results", 1);
			json.accumulate("errMsg", "组名不能为空！");
		}
		else
		{
			SysRole srole = null;
			srole = sysroleiDAO.findByRoleName(groupname);
			if(srole == null)
			{
				srole = new SysRole();
				String rid = CommonUtil.RandomStrNumDate(4);
				srole.setRid(rid);
				srole.setRoleName(groupname);
				srole.setRoleDesc(groupdesc);
				srole.setIsdel("1");
				srole.setStatus(1);
				srole.setUsercount(Integer.parseInt(usercount));
				flag=sysroleiDAO.save(srole);
				SysRole role = sysroleiDAO.findById(rid);
				SysAdmininfo user = new SysAdmininfo();
				String uid = CommonUtil.RandomStrNumDate(4);//随机ID
				user.setUid(uid);
				user.setSysRole(role);
				user.setUserName(groupname);
				user.setNickName(groupname);
				user.setUserPwd(grouppwd);
				user.setCreateTime(new Date());
				user.setHigher("admin");
				user.setType(2);//管理权限
				user.setIsdel("1");
				flag = sysadmininfoiDAO.save(user);
				if(flag)
				{
					json.accumulate("results", 0);
					json.accumulate("errMsg", "注册成功");
				}
				else
				{
					json.accumulate("results", 3);
					json.accumulate("errMsg", "注册失败");
				}
			}
			else
			{
				json.accumulate("results", 2);
				json.accumulate("errMsg", "组名重复！");
			}
		}	
		return json;
	}
	//根据组id查询组信息
	public SysRole findRoleByRid(String rid){
		
		SysRole role = null;
		role = sysroleiDAO.findById(rid);
		return role;
	}
	//用户注册
	public JSONObject userRegist(String groupname,String username,String nickname,String userpwd,String days)
	{
		JSONObject json=new JSONObject();
		boolean flag = false;
		if(days ==null || days.equals(""))
		{
			days="3650";//默认10年
		}
		if(userpwd ==null || userpwd.equals(""))
		{
			userpwd="123456";
		}
		if(nickname ==null || nickname.equals(""))
		{
			nickname=username;
		}
		if(groupname.equals("") || username.equals(""))
		{
			json.accumulate("results", 1);
			json.accumulate("errMsg", "组名/用户名不能为空！");
		}
		else
		{
			SysRole srole = null;
			srole = sysroleiDAO.findByRoleName(groupname);
			if(srole != null)
			{
				
				SysAdmininfo user = null;
				user =sysadmininfoiDAO.FindUserByGroupNameAndUsername(groupname, username);
				if(user == null)
				{
					user=new SysAdmininfo();
					Date now = new Date();
					String uid = CommonUtil.RandomStrNumDate(4);//随机ID
					user.setUid(uid);
					user.setSysRole(srole);
					user.setUserName(username);
					user.setNickName(nickname);
					user.setUserPwd(userpwd);
					user.setCreateTime(now);
					user.setHigher(srole.getRoleName());
					user.setType(3);//管理权限
					Long time = now.getTime();
					Long day = Long.parseLong(days)*24*60*60*1000;
					time +=day;
					user.setUserClosedate(time+"");				
					user.setIsdel("1");
					flag = sysadmininfoiDAO.save(user);
					if(flag)
					{
						json.accumulate("results", 0);
						json.accumulate("errMsg", "注册成功");
					}
					else
					{
						json.accumulate("results", 2);
						json.accumulate("errMsg", "注册失败");
					}
				}
				else
				{
					json.accumulate("results", 3);
					json.accumulate("errMsg", "用户名已经存在");
				}
			}
			else
			{
				json.accumulate("results", 1);
				json.accumulate("errMsg", "组名不正确！");
			}
		}	
		return json;
	}	
	//登录判断
	public Json loginInfo(String groupname,String username,String pwd,String devicenum){
		Json json = new Json();
		SysAdmininfo user = null;
		if(groupname!=null && username!=null && pwd!=null && devicenum!=null && !pwd.equals("") && !groupname.equals("") && !devicenum.equals("") && !username.equals(""))
		{
			System.out.println("用户名"+username);
			System.out.println("用户名"+pwd);
			//根据组名、用户名、密码查询出用户的信息
			user = sysadmininfoiDAO.FindUserByGroupNameAndUsername(groupname, username,pwd);
			System.err.println("用户查询成功。。。。。。。。。。。。");
			SysAdmininfoMobile mobile = null;
			SysAdmininfoMobile mobile1 = null;
			if(user != null){
				if(devicenum !=null && !devicenum.equals("")){
					//根据用户的id查询出用户的设备
					mobile = sysadmininfomobileiDAO.FindAdminMobileByUserid(user.getUid());
					System.out.println("根据用户id查找用户名设备"+mobile);
					//根据用户的组名、设备号码查询出设备
					mobile1 = sysadmininfomobileiDAO.FindAdminMobileByGroupidAndDeviceNum(user.getSysRole().getRid(),devicenum);
				}
				Date current_time = new Date();// 获取系统当前时间
				Long current = current_time.getTime();
				//对比时间是否过期
				Long time = current-Long.parseLong(user.getUserClosedate());
				if(user.getType().equals("2") || user.getType().equals("1"))
				{
					json.setResults("6");
					json.setErrMsg("该账户不能在手机设备上登陆！");
				}
				else
				{					
					String oldpwd = user.getUserPwd();
					if(mobile!=null){
						if(oldpwd.equals(pwd))
						{
							json.setResults("0");
							json.setErrMsg("登陆成功");
							json.setItems(mobile.getUserType());
							json.setMobile(mobile);
							json.setAdmin(user);
						}
						else
						{
							json.setResults("1");
							json.setErrMsg("密码错误");
						}
					}else {
						if(oldpwd.equals(pwd)){
							json.setResults("0");
							json.setErrMsg("登陆成功");
							json.setItems("0");
							json.setAdmin(user);
						}else{
							json.setResults("1");
							json.setErrMsg("密码错误");
						}
					}
						
										
					
					/*//绑定用户
					if(mobile!=null )
					{	//用户类型
						String usertype = mobile.getUserType();
						//判断当前登录的设备号是否在和根据用户id查询出的设备好一致。
						if(!mobile.getDeviceNum().equals(devicenum))
						{						
							json.setResults("9");
							json.setErrMsg("该账号已经绑定过设备！");
						}
						else
						{
							if(usertype.equals("1"))
							{
								if(oldpwd.equals(pwd))
								{
									json.setResults("0");
									json.setErrMsg("登陆成功");
									json.setItems(mobile.getUserType());
									json.setMobile(mobile);
									json.setAdmin(user);
								}
								else
								{
									json.setResults("1");
									json.setErrMsg("密码错误");
								}
							}
							else
							{
								
								if(time<0)
								{
									if(oldpwd.equals(pwd))
									{
										json.setResults("0");
										json.setErrMsg("登陆成功");
										json.setMobile(mobile);
										json.setItems(mobile.getUserType());
										json.setAdmin(user);
									}
									else
									{
										json.setResults("1");
										json.setErrMsg("密码错误");
									}
								}
								else
								{
									if(!oldpwd.equals(pwd))
									{
										json.setResults("1");
										json.setErrMsg("密码错误");
									}
									else
									{
										json.setResults("2");
										json.setAdmin(user);
										json.setMobile(mobile);
										json.setErrMsg("试用用户设备，设备已经过期，请点击申请授权！");
									}									
								}						
							}
						}
					}
					else
					{
						//未绑定设备的用户，比如第一次登录，查看此设备是否已经登录过
						if(mobile1!=null)
						{
							json.setResults("10");
							json.setErrMsg("该设备已经跟用户绑定！");
						}
						else
						{	
							if(time<0)
							{
								if(oldpwd.equals(pwd)){
									json.setResults("0");
									json.setErrMsg("登陆成功");
									json.setItems("0");
									json.setAdmin(user);
								}else{
									json.setResults("1");
									json.setErrMsg("密码错误");
								}
							}
							else
							{
								json.setResults("7");
								json.setErrMsg("用户已过期，在此设备无法登录！");
							}
						}
					}*/
				}
			}
			else
			{
				json.setResults("1");
				json.setErrMsg("用户名或密码错误！");
			}
		}
		else
		{
			json.setResults("8");
			json.setErrMsg("参数有空项！");
		}
		return json;
	}
	//登陆成功后保存设备信息
	public void saveMobileInfo(String uid,String auth,String usertype,String devicenum,String devicetoken,String devicesys,String devicetype,String pushtype,String servicecode)
	{
		boolean flag = false;
		SysAdmininfoMobile mobile = new SysAdmininfoMobile();
		mobile.setUid(uid);
		mobile.setUserType(usertype);
		mobile.setDeviceNum(devicenum);
		mobile.setDeviceSys(devicesys);
		mobile.setDeviceToken(devicetoken);
		mobile.setDeviceType(devicetype);
		mobile.setIsauth(auth);
		mobile.setPushType(pushtype);
		mobile.setPushornot("1");
		mobile.setServicecode(servicecode);
		SysAdmininfoMobile mobile_temp = sysadmininfomobileiDAO.FindAdminMobileByObject(mobile);
		System.out.println("mobile_temp="+mobile_temp);
		if(mobile_temp == null)
		{
			flag=sysadmininfomobileiDAO.save(mobile);
		}
		else
		{
			mobile.setSeqid(mobile_temp.getSeqid());
			flag=sysadmininfomobileiDAO.save(mobile);
		}
				
	}
	//ios退出系统
	public JSONObject ioslogout(String groupname,String username,String userpwd,String devicenum)
	{
		boolean flag = true;
		JSONObject jsonobj=new JSONObject();
		SysAdmininfo user = null;
		user = sysadmininfoiDAO.FindUserByGroupNameAndUsername(groupname,username,userpwd);
		if(user!=null)
		{
			SysAdmininfoMobile mobile = sysadmininfomobileiDAO.FindAdminMobileByUseridAndDeviceNum(user.getUid(), devicenum);
			mobile.setPushornot("0");
			flag = sysadmininfomobileiDAO.save(mobile);
			if(flag)
			{
				jsonobj.accumulate("results", 0);
				jsonobj.accumulate("errMsg", "更新成功");
			}
			else
			{
				jsonobj.accumulate("results", 1);
				jsonobj.accumulate("errMsg", "更新失败");
			}
		}
		else
		{
			jsonobj.accumulate("results", 1);
			jsonobj.accumulate("errMsg", "未找到此用户！");
		}
		return jsonobj;
	}	
	//更新用户地理位置相关信息
	public JSONObject updateUser(String uid,String longitude,String latitude,String countrycode,String countryname,String province,String city,String counties)
	{
		boolean flag = false;
		JSONObject jsonobj=new JSONObject();
		SysAdmininfo user = new SysAdmininfo();
		user = sysadmininfoiDAO.FindUserByUid(uid);
		if(latitude!=null && !latitude.equals(""))
			user.setLatitude(latitude);
		if(longitude!=null && !longitude.equals(""))
			user.setLongitude(longitude);
		if(city!=null && !city.equals(""))
			user.setCity(city);
		if(countrycode!=null && !countrycode.equals(""))
			user.setCountrycode(countrycode);
		if(countryname!=null && !countryname.equals(""))
			user.setCountryname(countryname);
		if(province!=null && !province.equals(""))
			user.setProvince(province);
		if(counties!=null && !counties.equals(""))
			user.setCounties(counties);
		Date now = new Date();
		user.setUpdateTime(now);
		flag = sysadmininfoiDAO.save(user);
		System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
		System.out.println(flag);
		if(flag)
		{
			jsonobj.accumulate("results", 0);
			jsonobj.accumulate("errMsg", "更新成功");
		}
		else
		{
			jsonobj.accumulate("results", 1);
			jsonobj.accumulate("errMsg", "更新失败");
		}
		return jsonobj;
	}
	//根据用户id查询设备信息
	public SysAdmininfoMobile findMobileByUserid(String uid){
		
		SysAdmininfoMobile mobile = null;
		mobile = sysadmininfomobileiDAO.FindAdminMobileByUserid(uid);
		return mobile;
	}
	//根据用户id查询设备信息
	public SysAdmininfo findUserByUserid(String uid){
		
		SysAdmininfo admin = null;
		admin = sysadmininfoiDAO.FindUserByUid(uid);
		return admin;
	}
	//登陆成功后保存设备信息
	public boolean saveMobile(SysAdmininfoMobile mobile)
	{
		boolean flag = false;
		flag=sysadmininfomobileiDAO.save(mobile);
		return flag;
	}
	//一键获取登录相关信息
	public List<VloginList> findAdminInfoByDeviceNum(String devicenum)
	{
		List<VloginList> loginlist=sysadmininfoiDAO.FindAdminInfoByDeviceNum(devicenum);
		return loginlist;
	}
	//视频服务器post视频信息接口---根据组名用户名列出用户基本信息
	public SysAdmininfo findUserByGroupNameAndUserName(String groupname,String username){
		
		SysAdmininfo user = null;
		user = sysadmininfoiDAO.FindUserByGroupNameAndUsername(groupname,username);
		return user;
	}
	
	
	public SysAdmininfo findByUserName(String username) {
		SysAdmininfo user=sysadmininfoiDAO.findByUserName(username);
		return user;
	}
	
	
}
